#analysis - logit
#alan yan
#2-9-22

####setup####
#clear environment
rm(list = ls())

#load packages
pacman::p_load(tidyverse,
               DeclareDesign,
               stargazer,
               hrbrthemes,
               MASS,
               sandwich,
               lmtest)

#load data
dt <- read_rds("02-framing-experiment/data/clean-data-stacked-pol")

#### analysis - support ####
#### *pooled ####
dt %>%
  filter(
    policy %in% c("codeterm",
                  "elections",
                  "esops")
  ) %>%
  polr(formula = factor(support) ~ treatment + policy, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy %in% c("codeterm", "elections", "esops")])) -> workplace.sup

#### *codeterm ####
dt %>%
  filter(
    policy %in% c("codeterm")
  ) %>%
  polr(formula = factor(support) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "codeterm"])) -> codeterm.sup


#### *elections ####
dt %>%
  filter(
    policy %in% c("elections")
  ) %>%
  polr(formula = factor(support) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "elections"])) -> elections.sup

#### *esops ####
dt %>%
  filter(
    policy %in% c("esops")
  ) %>%
  polr(formula = factor(support) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "esops"])) -> esops.sup

#### *stargazer ####
stargazer(workplace.sup,
          codeterm.sup,
          elections.sup,
          esops.sup,
          omit = "policy",
          covariate.labels = c("Benefit",
                               "Cost",
                               "Cost and benefit")
          )

#### analysis - responsibility ####
#### *pooled ####
dt %>%
  filter(
    policy %in% c("codeterm",
                  "elections",
                  "esops")
  ) %>%
  polr(formula = factor(resp) ~ treatment + policy, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy %in% c("codeterm", "elections", "esops")])) -> workplace.res

#### *codeterm ####
dt %>%
  filter(
    policy %in% c("codeterm")
  ) %>%
  polr(formula = factor(resp) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "codeterm"])) -> codeterm.res


#### *elections ####
dt %>%
  filter(
    policy %in% c("elections")
  ) %>%
  polr(formula = factor(resp) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "elections"])) -> elections.res

#### *esops ####
dt %>%
  filter(
    policy %in% c("esops")
  ) %>%
  polr(formula = factor(resp) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "esops"])) -> esops.res

#### *stargazer ####
stargazer(workplace.res,
          codeterm.res,
          elections.res,
          esops.res,
          omit = "policy",
          covariate.labels = c("Benefit",
                               "Cost",
                               "Cost and benefit")
)

#### analysis - power ####
#### *pooled ####
dt %>%
  filter(
    policy %in% c("codeterm",
                  "elections",
                  "esops")
  ) %>%
  polr(formula = factor(power) ~ treatment + policy, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy %in% c("codeterm", "elections", "esops")])) -> workplace.pow

#### *codeterm ####
dt %>%
  filter(
    policy %in% c("codeterm")
  ) %>%
  polr(formula = factor(power) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "codeterm"])) -> codeterm.pow


#### *elections ####
dt %>%
  filter(
    policy %in% c("elections")
  ) %>%
  polr(formula = factor(power) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "elections"])) -> elections.pow

#### *esops ####
dt %>%
  filter(
    policy %in% c("esops")
  ) %>%
  polr(formula = factor(power) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "esops"])) -> esops.pow

#### *stargazer ####
stargazer(workplace.pow,
          codeterm.pow,
          elections.pow,
          esops.pow,
          omit = "policy",
          covariate.labels = c("Benefit",
                               "Cost",
                               "Cost and benefit")
)

#### analysis - work ####
#### *pooled ####
dt %>%
  filter(
    policy %in% c("codeterm",
                  "elections",
                  "esops")
  ) %>%
  polr(formula = factor(work) ~ treatment + policy, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy %in% c("codeterm", "elections", "esops")])) -> workplace.work

#### *codeterm ####
dt %>%
  filter(
    policy %in% c("codeterm")
  ) %>%
  polr(formula = factor(work) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "codeterm"])) -> codeterm.work


#### *elections ####
dt %>%
  filter(
    policy %in% c("elections")
  ) %>%
  polr(formula = factor(work) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "elections"])) -> elections.work

#### *esops ####
dt %>%
  filter(
    policy %in% c("esops")
  ) %>%
  polr(formula = factor(work) ~ treatment, Hess = TRUE) %>%
  coeftest(., vcov = vcovCL(., clusters = df$rid[df$policy == "esops"])) -> esops.work

#### *stargazer Tables S169 to S172####
stargazer(workplace.work,
          codeterm.work,
          elections.work,
          esops.work,
          omit = "policy",
          covariate.labels = c("Benefit",
                               "Cost",
                               "Cost and benefit")
)

